/*
 * Transferencia.java
 *
 * Created on 18 de septiembre de 2007, 09:41
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package edu.idei.filetree;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import javax.swing.JComponent;
import javax.swing.JTree;
import javax.swing.TransferHandler;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author Emilio
 */
public class FileNameTransferer extends TransferHandler {

	private static final long serialVersionUID = 1l;
	private JTree tree;
	private DefaultTreeModel model;
	private FileOrderStatus filesInfo;

	public FileNameTransferer(JTree tree, FileOrderStatus filesInfo) {
		this.tree = tree;
		this.filesInfo = filesInfo;
		this.model = (DefaultTreeModel) tree.getModel();
	}

	@Override
	public int getSourceActions(JComponent comp) {
		return FileNameTransferer.MOVE;
	}

	@Override
	public Transferable createTransferable(JComponent comp) {
		// Obtengo el nodo seleccionado
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getSelectionPath().getLastPathComponent();
		// Accedo al objeto File que contiene
		String selected = (String) node.getUserObject();
		if (selected != null) {
			// Creo un objeto transferible en base al path absoluto del archivo.
			return new StringSelection(selected);
		}
		return null;
	}

	/**
	 * 
	 * @param support
	 * @return
	 */
	@Override
	public boolean canImport(TransferHandler.TransferSupport support) {
		if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) ||
				!support.isDrop()) {
			return false;
		}
		JTree.DropLocation dropLocation =
				(JTree.DropLocation) support.getDropLocation();
		return dropLocation.getPath() != null;
	}

	@Override
	public boolean importData(TransferHandler.TransferSupport support) {
		if (!canImport(support)) {
			return false;
		}

		JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
		TreePath path = dropLocation.getPath();
		Transferable transferable = support.getTransferable();
		DefaultMutableTreeNode parentNode =
				(DefaultMutableTreeNode) path.getLastPathComponent();

		DefaultMutableTreeNode transferNode;
		try {
			String transferedFileID = (String) transferable.getTransferData(DataFlavor.stringFlavor);
			transferNode = filesInfo.getNodeForFile(transferedFileID);
			int childIndex = dropLocation.getChildIndex();
			if (childIndex == -1) {
				childIndex = parentNode.getChildCount() - 1;
			}
			parentNode.insert(transferNode, childIndex);
			model.nodeStructureChanged(parentNode);
			TreePath newPath = path.pathByAddingChild(transferNode);
			tree.setSelectionPath(newPath);
			tree.makeVisible(newPath);
			tree.scrollRectToVisible(tree.getPathBounds(newPath));
			filesInfo.write();
		} catch (Throwable th) {
			System.out.println(th);
		}
		return true;
	}
}
